import React from 'react';
import { Redirect } from 'dva/router';
import dynamic from 'dva/dynamic';
import NotFound from '@/components/404';
import NotAuthority from '@/components/403';

// 未启用
const checkPath = paths => {
    return new Promise((resolve, reject) => {
        let effectiveModels = [];
        paths.forEach((p, index) => {
            import(`@/${p}/models`)
                .then(() => {
                    effectiveModels.push(p);
                    if (paths.length - 1 === index) {
                        resolve(effectiveModels);
                    }
                })
                .catch(() => {});
        });
    });
};
// 未启用
const handlerModels = paths => {
    // 根据路由返回目录下有 models的目录 过滤没有 models的目录
    return new Promise(async (resolve, reject) => {
        const newPaths = await checkPath(paths);
        const newModels = newPaths.map(p => import(`@/${p}/models/`));
        resolve(newModels);
    });
};

export default app => {
    const dynamicWrapper = (path, models = []) => {
        /**
         * @ marams ...models ['pages/xxx', 'modules/xxx']
         * @ TODO 使用 Promise async await 可实现 目录下有 models文件就加载，没有就不加载。
         * @ TODO 但是 dynamic 不接收 Promise 类型的值
         * @ msg 每个层级的目录下都必须有一个 models.js 文件 需要注入过多的models需要手动在路由里配置
         */
        return dynamic({
            app,
            models: () => [path, ...models].map(p => import(`@/${p}/models`)),
            component: () => import(`@/${path}`)
        });
    };
    /**
     * Router Config
     * root config =====> 一级路由配置之后即在一级页面生效
     * 非root config ===>  二级及以下子路由 需要在对应的上一层路由进行生成路由
     * 方法把当前对应的路由配置传递进去
     */
    return [
        {
            path: '/',
            component: () => <Redirect to="/main" />
        },
        {
            path: '/main',
            component: dynamicWrapper('pages/main'),
            children: [
                {
                    path: '/main/',
                    component: () => <Redirect to="/main/dashboard" />
                },
                {
                    path: '/main/dashboard',
                    component: dynamicWrapper('pages/main/dashboard')
                },
                {
                    path: '/main/queryform',
                    component: dynamicWrapper('pages/main/queryform')
                },
                {
                    path: '/main/dataview',
                    component: dynamicWrapper('pages/main/dataview'),
                    children: [
                        {
                            path: '/main/dataview/',
                            component: () => (
                                <Redirect to="/main/dataview/trends" />
                            )
                        },
                        {
                            path: '/main/dataview/trends',
                            component: dynamicWrapper(
                                'pages/main/dataview/trends'
                            )
                        },
                        {
                            path: '/main/dataview/tables',
                            component: dynamicWrapper(
                                'pages/main/dataview/tables'
                            )
                        },
                        {
                            path: '/main/dataview/forms',
                            component: dynamicWrapper(
                                'pages/main/dataview/forms'
                            )
                        }
                    ]
                },
                {
                    path: '/main/manage',
                    component: dynamicWrapper('pages/main/manage')
                },
                {
                    path: '/main/403',
                    component: NotAuthority
                },
                {
                    path: '/main/*',
                    component: NotFound
                }
            ]
        },
        {
            path: '/user',
            component: dynamicWrapper('pages/user')
        },
        {
            path: '/*',
            component: () => <Redirect to="/" />
        }
    ];
};
